skip_docs
import '../../Fastfile'
default_platform(:ios)

before_all do
  if is_ci
    setup_ci
  end
end

platform :ios do
  app_id = "1:674907137625:ios:cafb9fb076a453c4d7f348" # The Firebase app ID
  app_identifier = "io.getstream.flutter" # The bundle identifier of your app

  desc "Download or Regenerate appstore provisioning profiles, Optionally register a new device"
  # Usage: bundle exec fastlane ios match_me register_device:true
  lane :match_me do |options|
    custom_match(
      api_key: appstore_api_key,
      app_identifier: [app_identifier],
      readonly: options[:readonly],
      register_device: options[:register_device]
    )
  end

  ### Build lanes

  desc "Build IPA file with specified parameters"
  # Usage: bundle exec fastlane ios build_ipa build_number:123 build_name:"1.0.0" export_method:"app-store" no_codesign:true
  lane :build_ipa do |options|
    # Fetch dependencies
    fetch_dependencies

    codesign_identity = {
      "ad-hoc" => "Apple Distribution",
      "app-store" => "Apple Distribution",
      "development" => "Apple Development"
    }

    profile = {
      "ad-hoc" => "match AdHoc #{app_identifier}",
      "app-store" => "match AppStore #{app_identifier}",
      "development" => "match Development #{app_identifier}"
    }

    export_files = {
      "ad-hoc" => "#{root_path}/ios/fastlane/export_options_firebase.plist",
      "app-store" => "#{root_path}/ios/fastlane/export_options_testflight.plist",
      "development" => "#{root_path}/ios/fastlane/export_options_testing.plist"
    }

    build_name = options[:build_name]
    build_number = options[:build_number]
    no_codesign = options[:no_codesign] || false
    export_method = options[:export_method] || "app-store"
    export_options_plist = export_files[export_method]

    # Note: This is a workaround for the issue with the flutter build ipa command
    # issue: https://github.com/flutter/flutter/issues/113977
    ENV["FLUTTER_XCODE_CODE_SIGN_IDENTITY"] = codesign_identity[export_method]
    ENV["FLUTTER_XCODE_PROVISIONING_PROFILE_SPECIFIER"] = profile[export_method]

    # Generate the full command
    command = "flutter build ipa --suppress-analytics --export-options-plist=#{export_options_plist}"
    command += " --build-name=#{build_name}" if build_name.to_s != ""
    command += " --build-number=#{build_number}" if build_number.to_s != ""
    command += " --no-codesign" if no_codesign

    # Build the app
    sh_on_root(command: command)
  end

  desc "Build and distribute app to Firebase App Distribution with auto-incrementing build number"
  # Usage: bundle exec fastlane ios distribute_to_firebase
  lane :distribute_to_firebase do
    match_me

    latest_release = firebase_app_distribution_get_latest_release(
      app: app_id,
      service_credentials_file: "#{root_path}/ios/firebase-service-account.json"
    )

    build_number = (latest_release&.dig(:buildVersion) || 0).to_i + 1
    build_ipa(export_method: "ad-hoc", build_number: build_number)

    firebase_app_distribution(
      app: app_id,
      ipa_path: "#{root_path}/build/ios/ipa/ChatSample.ipa",
      groups: "stream-testers, ios-stream-testers",
      release_notes: "Lots of amazing new features to test out!",
      service_credentials_file: "#{root_path}/ios/firebase-service-account.json"
    )
  end

  desc "Build and distribute app to TestFlight with auto-incrementing build number"
  # Usage: bundle exec fastlane ios distribute_to_testflight
  lane :distribute_to_testflight do
    match_me

    current_build_number = latest_testflight_build_number(
      api_key: appstore_api_key,
      app_identifier: app_identifier
    )

    build_number = (current_build_number || 0).to_i + 1
    build_ipa(export_method: "app-store", build_number: build_number)

    upload_to_testflight(
      api_key: appstore_api_key,
      distribute_external: true,
      notify_external_testers: true,
      ipa: "#{root_path}/build/ios/ipa/ChatSample.ipa",
      groups: ['Internal Testers', 'External Testers'],
      changelog: 'Lots of amazing new features to test out!',
      skip_waiting_for_build_processing: true,
    )
  end

  private_lane :appstore_api_key do
    @appstore_api_key ||= app_store_connect_api_key(
      key_id: 'MT3PRT8TB7',
      issuer_id: '69a6de96-0738-47e3-e053-5b8c7c11a4d1',
      key_content: ENV.fetch('APPSTORE_API_KEY', nil),
      in_house: false,
    )
  end
end